home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 215_01.zip / BBSCPORT.C < prev    next >
Text File  |  1980-01-01  |  6KB  |  311 lines

  1. /*
  2.     bbscport.c
  3.  
  4.     Support routines used by BBSc.c to access the modem port.
  5.     Used with the Godbout Interfacer IV I/O board.
  6.                 Mike Kelly
  7.  
  8.     03/09/83 v1.0    written
  9.     07/08/83 v1.0    Added code to look for modem carrier detect,
  10.             and exit() if not still connected.
  11. */
  12.  
  13. #include "bdscio.h"
  14. #include "bbscdef.h"
  15.  
  16.  
  17. #define LASTDATE  " 07/08/83 "
  18.  
  19. #define PGMNAME "BBSCPORT "
  20. #define VERSION " 1.0 "
  21.  
  22.  
  23. getuser()        /* inquires for which user # to use */
  24. {
  25.     int    user;
  26.  
  27.     printf("\nWhich user # to communicate with?  ==>");
  28.     while((user = getchar()) != '5' && user != '6' && user != '7')
  29.     {
  30.         printf("\b \b");    /* backspace,space,backspace */
  31.     }
  32.     putchar('\n');
  33.     user -= 48;    /* adjust from ascii */
  34.     portinit(user);    /* init i/o board */
  35. }
  36. /*    end of function        */
  37.  
  38.  
  39.  
  40. portinstat()    /*  returns 1 if no char, 0 if char waiting */
  41. {
  42.     char byte0;
  43.  
  44.     if (offline)
  45.     {
  46.         byte0 = inp(LCSTAT);    /* get local console status */
  47.     }
  48.     else
  49.     {
  50.         byte0 = inp(GBI4PSTAT);    /* get a status byte from port */
  51.         portcarrier(byte0);    /* check carrier status */
  52.     }
  53.  
  54.     if ((byte0 & SDAV) == SDAV)    /* mask all but char available */
  55.     {
  56.         return(0);    /* char waiting, so get out */
  57.     }
  58.     else
  59.     {
  60.         return(1);    /* no char waiting */
  61.     }
  62. }
  63. /*    end of function        */
  64.   
  65. char portin()        /* get one byte from the port */
  66. {
  67.     char byte;
  68.  
  69.     if (offline)
  70.     {
  71.         byte = toupper(inp(LCDATA));
  72.     }
  73.     else
  74.     {
  75.           byte = toupper(inp(GBI4PDATA));
  76.     }
  77.  
  78. #ifdef DEBUG
  79.     if (debug)
  80.     {
  81.         savepos();
  82.         to(25,12);
  83.         printf("byte in=%02x",byte);
  84.         retpos();
  85.     }
  86. #endif
  87.  
  88.     return(byte);
  89. }
  90. /*    end of function        */
  91.  
  92. portsin(buf,max)    /* get a line of input max. chars long */
  93. int    max;
  94. char    *buf;
  95. {
  96.     int    cnt,
  97.         byte;
  98.     char    bytex;
  99.  
  100.     cnt = 0;
  101.  
  102. #ifdef DEBUG
  103.     if (debug)
  104.     {
  105.         printf("in portsin\n");
  106.         printf("  max=%05d  cnt=%05d\n",max,cnt);
  107.     }
  108. #endif
  109.  
  110.     byte = FALSE;
  111.     while (++cnt <= max && byte != '\r')
  112.     {
  113.         while(portinstat());
  114.         while((byte = portin()) < ' ' || byte > '}')
  115.         {
  116.             if (byte == '\r')    /* carriage return */
  117.             {
  118.                 break;
  119.             }
  120.             if (byte == '\b' && cnt > 1)    /* backspace */
  121.             {
  122.                 while (portoutstat());
  123.                 portout(byte);
  124.                 while (portoutstat());
  125.                 portout(' ');
  126.                 while (portoutstat());
  127.                 portout(byte);
  128.                 *buf--;    /* backout last char */
  129.                 cnt--;    /* decrement count too */
  130.             }
  131.             while(portinstat());
  132.         }
  133.         if (byte != '\r')
  134.         {
  135.             *buf++ = byte;
  136.         }
  137.         portout(byte);    /* echo good chars only */
  138.     }
  139.  
  140.     *buf++    = '\0';            /* tag \0 on end */
  141.  
  142. #ifdef DEBUG
  143.     if (debug)
  144.     {
  145.         printf("  cnt=%05d\n",cnt);
  146.     }
  147. #endif
  148. }
  149. /*    end of function        */
  150.  
  151. portoutstat()            /* returns 0 if buffer empty, 1 if not empty */
  152. {
  153.     char byte0;
  154.  
  155.         byte0 = inp(GBI4PSTAT);    /* get status from port */
  156.         portcarrier(byte0);    /* check carrier status */
  157. #ifdef DEBUG
  158. /*        if (debug)
  159.         {
  160.             if (statcnt++ > STATMAX)
  161.             {
  162.                 savepos();
  163.                 to(25,23);
  164.                 printf("stat out=%02x",byte0);
  165.                 retpos();
  166.             }
  167.         }
  168. */
  169. #endif
  170.         if ((byte0 & SEMPTY) == SEMPTY)    /* mask all but xmit */
  171.         {                /*  buffer empty bit */
  172.             return(0);    /* buffer empty so get out */
  173.         }
  174.         else
  175.         {
  176.             return(1);    /* buffer not empty */
  177.         }
  178. }
  179. /*    end of function        */
  180.  
  181. portout(byte)        /* send one byte to the port */
  182. char byte;        /*  return CTL_K for those times want to check if */
  183. {            /*  the person wants to stop sending using */
  184.     char    byte0;    /*  <ctl>-K or K (see porttype), else return 0 */
  185.  
  186. #ifdef DEBUG
  187.     if (debug)
  188.     {
  189.         savepos();
  190.         to(25,35);
  191.         printf("byte out=%02x",byte);
  192.         retpos();
  193.     }
  194. #endif
  195.  
  196.     if (portinstat() == 0)            /* == 0 means char waiting */
  197.     {                
  198.         if ((byte0 = portin()) == 0x13)    /* ctl-S?, then */
  199.         {                /*  simulate xon/xoff */
  200.             while (portinstat());    /* wait for any next char */
  201.             portin();        /* gobble up the char */
  202.         }
  203.         if (byte0 == CTL_K || byte0 == 'K')    /* look for stop */
  204.         {                    /*  sending key from */
  205. /*            return(CTL_K);            /*  the port */
  206. */
  207.             stop_that = TRUE;    /* set switch on */
  208.         }
  209.     }
  210.     outp(GBI4PDATA,byte);    /* send the byte to port */
  211.     outp(LCDATA,byte);    /* to local console too */
  212.     return(OK);            /* all ok */
  213. }
  214. /*    end of function        */
  215.   
  216. portsout(string)    /* send a string to the port */
  217. char *string;
  218. {
  219.     char byte;
  220.  
  221. #ifdef DEBUG
  222.     if (debug)
  223.     {
  224.         printf("\nportsout string out=%s\n",string);
  225.     }
  226. #endif
  227.     while (byte = (*string++))
  228.     {
  229.         while (portoutstat());    /* returns false when ready */
  230.           portout(byte);        /* send one byte at a time */
  231.     }
  232. }
  233. /*    end of function        */
  234.  
  235. porttype(buf)        /* type a file to the port */
  236. char    *buf;
  237. {
  238.     char    byte,
  239.         byte2;
  240.  
  241.     portsout("\r\nType ctl-K or K to skip this\r\n\n");
  242.     while ((byte = getc(buf)) != EOF && byte != CPMEOF)
  243.     {
  244.         while (portoutstat());    /* wait for port to be free */
  245.         portout(byte);
  246.         if (stop_that)        /* received ctl-K or K */
  247.         {
  248.             while(portoutstat());
  249.             portsout("\n\r");
  250.             stop_that = FALSE;    /* reset switch */
  251.             return;        /* nuf's enough */
  252.         }
  253.     }
  254. }
  255. /*    end of function        */
  256.  
  257.  
  258. portinit(user)                /* init godbout IV i/o board */
  259. char    user;
  260. {
  261.  
  262. #ifdef DEBUG
  263. /*    if (debug)
  264.     {
  265.         savepos();
  266.         to(25,67);
  267.         printf("user=%02x",user);
  268.         retpos();
  269.     }
  270. */
  271. #endif
  272.  
  273.     outp(GBI4PSEL,user);        /* tell who is calling */
  274.     outp(GBI4PMODE,0x6e);
  275.     outp(GBI4PMODE,0x75);            /* 300 baud */
  276.     outp(GBI4PCMD,0x27);
  277. }
  278. /*    end of function        */
  279.  
  280. char gobble()                /* gobble up any answer */
  281. {
  282.     int    cnt;
  283.  
  284.     while (cnt++ < 20)
  285.     {
  286.         portin();
  287.     }
  288. }
  289. /*    end of function        */
  290.  
  291. portcarrier(byte0)        /* check modem for carrier */
  292. char    byte0;            /*  exit() if not still connected */
  293. {                /* gets passed port status byte */
  294.  
  295.     if (offline)        /* don't check if in offline mode */
  296.     {
  297.         return;
  298.     }
  299.  
  300.     if ((byte0 & SCARRIER) == SCARRIER)
  301.     {
  302.         return;            /* yup, still there */
  303.     }
  304.     exit(0);            /* nope, lost carrier, so get out */
  305.                     /*  with some form of gracefulness */
  306. }
  307. /*    end of function        */
  308.  
  309.  
  310. /*    end of program      */
  311.